package com.tqjc.common.websocket.service.impl;

import com.alibaba.fastjson.JSON;

import com.tqjc.common.websocket.service.MessageSendService;
import com.tqjc.system.core.redis.TaskRedisApi;
import com.tqjc.utils.PlcOperator;
import com.tqjc.work.TaskStatus;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


@Service
public class MessageSendServiceImpl implements MessageSendService {
    @Resource
    private SimpMessageSendingOperations simpMessageSendingOperations;


    @Resource
    PlcOperator plcOperator;

    @Resource
    TaskStatus taskStatus;


    @Resource
    TaskRedisApi taskRedisApi;





    @Override
    @Scheduled(cron = "0/1 * *  * * ? ")
    public void sendPlcData() {
        /*//获取所有的采集连接对象
        List<ConnectInfo> allConnectList = connectInfoService.list(null);
        if (allConnectList.size() > 0) {
            for (ConnectInfo connectInfo : allConnectList) {
                String connectSn = connectInfo.getSn();
//查询点位数据
                PlcResult plcResult = plcOperator.readSbca(connectSn);
                if (plcResult.isSuccess()) {
                    simpMessageSendingOperations.convertAndSend("/topic/plcData/" + connectSn, JSON.toJSONString(plcResult.getData()));
                }
            }
        }*/
    }


    @Override
    @Scheduled(cron = "0/2 * *  * * ? ")
    public void sendTaskData() {
        /*//获取所有的天车对象
        List<CraneInfo> craneInfoList = craneInfoService.list(null);
        if (craneInfoList.size() > 0) {
            for (CraneInfo craneInfo : craneInfoList) {
                String craneType = craneInfo.getName();
                WorkStatus workStatus = taskStatus.getWorkStatusByCraneType(craneType);
                try {
                    simpMessageSendingOperations.convertAndSend("/topic/taskData/" + URLEncoder.encode(craneType, StandardCharsets.UTF_8), JSON.toJSONString(workStatus));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }*/
    }

    @Override
    @Scheduled(cron = "0/3 * *  * * ? ")
    public void refreshCraneStatus() {
        /*List<String> craneList = NormalConst.CRANE_LIST;
        for (String s : craneList) {
            //TODO 从PLC中获取天车故障信息；
            int status=0;
            craneWorkFactory.getCraneWorkImpl(s).getData().setCraneStatus(status);
            if(status!=0){  //天车故障，停止自动任务；  0为正常，非0为故障
                taskRedisApi.set(s+"_status",status);
                craneWorkFactory.getCraneWorkImpl(s).getData().setStartLogo(false);
            }

        }*/
    }

}
